package com.jhgsys.internal.sypq.service.impl;

import com.jhgsys.internal.sypq.entity.TaskAttach;
import com.jhgsys.internal.sypq.entity.TaskTest;
import com.jhgsys.internal.sypq.mapper.TaskTestMapper;
import com.jhgsys.internal.sypq.service.ITaskTestService;
import com.jhgsys.internal.common.entity.QueryRequest;
import com.jhgsys.internal.common.utils.FebsUtil;
import com.jhgsys.internal.system.entity.User;
import org.apache.poi.ss.formula.functions.T;
import org.apache.shiro.SecurityUtils;
import com.jhgsys.internal.common.entity.FebsConstant;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.annotation.Propagation;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.toolkit.StringPool;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 试验内容 Service实现
 *
 * @author ganjun
 * @date 2024-11-18 11:13:09
 */
@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class TaskTestServiceImpl extends ServiceImpl<TaskTestMapper, TaskTest> implements ITaskTestService {

    /**
     * 查询（分页）
     * @author ganjun
     * @date 2024-11-18 11:13:09
     * @param request QueryRequest
     * @param taskTest taskTest
     * @return IPage<TaskTest>
    */
    @Override
    public IPage<TaskTest> findTaskTestPage(QueryRequest request, TaskTest taskTest) {
        LambdaQueryWrapper<TaskTest> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TaskTest::getDeletedFlg, 0);
        // TODO 设置查询条件

        Page<TaskTest> page = new Page<>(request.getPageNum(), request.getPageSize());
        FebsUtil.pageWrapper(page, request);
        return this.page(page, queryWrapper);
    }

    /**
     * 查询（所有）
     * @author ganjun
     * @date 2024-11-18 11:13:09
     * @param taskTest taskTest
     * @return List<TaskTest>
     */
    @Override
    public List<TaskTest> findTaskTestList(TaskTest taskTest) {
	    LambdaQueryWrapper<TaskTest> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TaskTest::getDeletedFlg, 0);
        queryWrapper.isNull(TaskTest::getDeletedTime);
        queryWrapper.eq(TaskTest::getTaskcode,taskTest.getTaskcode());
		// TODO 设置查询条件
		return this.baseMapper.selectList(queryWrapper);
    }

    /**
    * 根据ID查询
    * @author ganjun
    * @date 2024-11-18 11:13:09
    * @param id
    * @return TaskTest
    */
    @Override
    public TaskTest findTaskTestById(String id){
        return this.baseMapper.selectById(id);
    }

    /**
     * 新增
     * @author ganjun
     * @date 2024-11-18 11:13:09
     * @param taskTest taskTest
     */
    @Override
    @Transactional
    public boolean createTaskTest(TaskTest taskTest) {
        return this.save(taskTest);
    }

    /**
     * 修改
     * @author ganjun
     * @date 2024-11-18 11:13:09
     * @param taskTest taskTest
     */
    @Override
    @Transactional
    public boolean updateTaskTest(TaskTest taskTest) {
        return this.updateById(taskTest);
    }

    /**
     * 逻辑删除
     * @author ganjun
     * @date 2024-11-18 11:13:09
     * @param taskTest taskTest
     * @param taskTestIds taskTestIds
     */
    @Override
    @Transactional
    public boolean deleteTaskTest(TaskTest taskTest,String taskTestIds) {
        boolean result = false;
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        List<String> list = Arrays.asList(taskTestIds.split(StringPool.COMMA));

        taskTest.setDeletedBy(user.getUserName());
        taskTest.setDeletedTime(new Date());
        taskTest.setDeletedFlg(FebsConstant.INVALID);
        int update = this.baseMapper.update(taskTest,new LambdaQueryWrapper<TaskTest>().in(TaskTest::getId,list));
        if (update > 0 ){
            result = true;
        }
        return result;
	}

    /**
    * 物理删除
    * @author ganjun
    * @date 2024-11-18 11:13:09
    * @param taskTestIds taskTestIds
    */
    @Override
    @Transactional
    public boolean physicallyDeleteTaskTest(String taskTestIds) {
        List<String> list = Arrays.asList(taskTestIds.split(StringPool.COMMA));
        LambdaQueryWrapper<TaskTest> wapper = new LambdaQueryWrapper<>();
        wapper.in(TaskTest::getId,list);
        return this.remove(wapper);
    }

   public  boolean physicallyDeleteByTaskcode(String  taskcode){
        LambdaQueryWrapper<TaskTest> wapper = new LambdaQueryWrapper<>();
        wapper.eq(TaskTest::getTaskcode,taskcode);
        return this.remove(wapper);

    }
}
